//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using CommunityServer.Components;


namespace CommunityServer.Components
{
	/// <summary>
	/// The CachedFeed is a cacheable container for our rss feed(s). Instead of requesting the cache data, processing it,
	/// and creating an XML document on each request, we will store the actually Rss document as a cached string.
	/// 
	/// Generally, it will be returned to the client by calling Response.Write(feed.Xml)
	/// </summary>
	public class CachedFeed
	{
		/// <summary>
		/// Creates a new cached feed.
		/// </summary>
		/// <param name="lastModified">The date/time of the last entry in the feed.</param>
		/// <param name="etag">The tag value of the feed.</param>
		/// <param name="xml">The feed.</param>
		public CachedFeed(DateTime lastModified, string etag, string xml)
		{
			// Just incase the user changes timezones after a post
			if (lastModified > DateTime.Now)
			{
				this.lastModified = DateTime.Now;
			}
			else
			{
				this.lastModified = lastModified;
			}

			if (etag == null)
			{
				//if we did not set the etag, just use the LastModified Date
				this.etag = this.lastModified.ToString();
			}
			else
			{
				this.etag = etag;
			}

			this.xml = xml;
		}

		/// <summary>
		/// The date/time of the last entry in the feed.
		/// </summary>
		private DateTime lastModified;

		/// <summary>
		/// The date/time of the last entry in the feed.
		/// </summary>
		public DateTime LastModified
		{
			get{ return lastModified; }
		}

		/// <summary>
		/// The tag value of the feed.
		/// </summary>
		private string etag;

		/// <summary>
		/// The tag value of the feed.
		/// </summary>
		public string Etag
		{
			get { return etag; }
		}

		/// <summary>
		/// The feed.
		/// </summary>
		private string xml;

		/// <summary>
		/// The feed.
		/// </summary>
		public string Xml
		{
			get { return xml; }
		}

		/// <summary>
		/// Checks to see if the Requesting client has the most recent version of the feed
		/// </summary>
		/// <param name="CurrentEtag">Etag header sent with Response (If-None-Match)</param>
		/// <param name="CurrentLastModifiedDate">LastModified Data header sent with Response (If-Modified-Since)</param>
		/// <returns></returns>
		public bool IsCacheOK(string CurrentEtag, string CurrentLastModifiedDate)
		{
			bool isCache = false;
			string lastModified = this.LastModified.ToUniversalTime().ToString("r");

			if (CurrentLastModifiedDate != null && CurrentEtag != null)
			{
				isCache = (lastModified == CurrentLastModifiedDate) && (this.Etag == CurrentEtag);
			}
			else if (CurrentLastModifiedDate != null)
			{
				isCache = (lastModified == CurrentLastModifiedDate);
			}
			else if (CurrentEtag != null)
			{
				isCache = (this.Etag == CurrentEtag);
			}

			return isCache;
		}
	}
}